package com.iheima.reggie_take.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iheima.reggie_take.common.R;
import com.iheima.reggie_take.dto.SetmealDto;
import com.iheima.reggie_take.entity.Category;
import com.iheima.reggie_take.entity.Setmeal;
import com.iheima.reggie_take.service.CategoryService;
import com.iheima.reggie_take.service.SetmealDishService;
import com.iheima.reggie_take.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * ClassName: SetmealController
 * Package: com.iheima.reggie_take.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-01-18
 * Time: 15:04
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 涉及两张表 套餐表 还有套餐和菜品的关系表添加数据
     * @param setmealDto
     * @return
     */
    //添加套餐
    // Post localhost:8080/setmeal
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐 {}" ,setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    //GET
    //localhost:8080/setmeal/page?page=1&pageSize=10
    //套餐信息的分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        //改造一下 使页面显示完整 在SetmealDto中有categoryName 就是页面需要的
        Page<SetmealDto> dtoPage = new Page<>();
        //将pageInfo 拷贝到 dtoPage

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //添加排序条件,根据更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);
        //pageInfo的泛型是Setmeal 但是实体setmeal存储的是分类的id
        // 但是页面上展示的是 分类的名称

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        //拷贝 忽略records 不同拷贝(泛型不一样) 那么就要自己设置
        List<Setmeal> recodes = pageInfo.getRecords();

        List<SetmealDto> list = recodes.stream().map((item) ->{
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id 通过 这个分类id来查询分类名称
            Long categoryId = item.getCategoryId();
            //
            Category category = categoryService.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());


        dtoPage.setRecords(list);

        return R.success(dtoPage);
    }

    //  删除 套餐
    //  DELETE
    //	http://localhost:8080/setmeal?ids=1615691637364596738,1231231231231
    //  批量删除是 id,id,id
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        //删除套餐 并且 删除对应的关联关系
        setmealService.removeWithDish(ids);

        return R.success("套餐数据删除成功!");
    }

    /**
     * 这俩都是更新状态操作，一个启售一个停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> startSale(Long ids){
        Setmeal setmeal=setmealService.getById(ids);
        setmeal.setStatus(0);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getId, ids);
        setmealService.update(setmeal, lambdaQueryWrapper);
        return R.success("更新状态为启售");
    }
    @PostMapping("/status/1")
    public R<String> stopSale(Long ids){
        Setmeal setmeal=setmealService.getById(ids);
        setmeal.setStatus(1);
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getId, ids);
        setmealService.update(setmeal, lambdaQueryWrapper);
        return R.success("更新状态为停售");
    }



    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }


}
